<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Category Model
 *
 * @package		SimplyPost
 * @subpackage	Category
 * @category	Model
 * @author		Pascal Kriete
 */

class Cat_model extends Model {
	
	/**
	 * Constructor
	 *
	 * @access	public
	 */
    function Cat_model()
    {
        parent::Model();
    }

	// --------------------------------------------------------------------

	/**
	 * Get a single row
	 *
	 * @access	public
	 * @param	integer	row id
	 * @return	mixed	row data
	 */
	function get($id)
	{
		$query = $this->db->get_where('categories', array('id' => $id), 1, 0);
		return $query->row_array();
	}
	
	// --------------------------------------------------------------------

	/**
	 * Get all rows
	 *
	 * @access	public
	 * @return	mixed
	 */
	function get_all()
	{		
		$this->db->select('id, name');
		$query = $this->db->get('categories');
		
		if ($query->num_rows < 1)
            return FALSE;
		else
			return $query->result_array();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Check if a category exists
	 *
	 * @access	public
	 * @param	string  category name
	 * @return	bool
	 */
	function exists($name)
	{
		$query = $this->db->getwhere('categories', array('name' => $name), 1, 0);
		if($query->num_rows() == 1)
			return TRUE;
		else
			return FALSE;
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Create a new category
	 *
	 * @access	public
	 * @param	string	new category name
	 * @return	void
	 */
	function create($name)
	{
		$this->db->insert('categories', array('name' => $name));		
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Change a category name
	 *
	 * @access	public
	 * @param	integer	category id
	 * @param	string	new name
	 * @return	void
	 */
	function update($id, $name)
	{
		$this->db->where('id', $id);
		$this->db->update('categories', array('name' => $name));		
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Delete a category
	 *
	 * @access	public
	 * @param	integer  category id
	 * @return	void
	 */
	function delete($id) {
		$this->db->select('id, parent_id');
		$this->db->where('parent_id', $id);
		$query = $this->db->get('forums');
		
		foreach ($query->result_array() as $row)
		{
			$this->db->select('id, parent_id');
			$this->db->where('parent_id', $row['id']);
			$query2 = $this->db->get('topics');
			
			foreach ($query2->result_array() as $row2)
			{
				$this->db->delete('posts', array('parent_id' => $row2['id']));
			}
			
			$this->db->delete('topics', array('parent_id' => $row['id']));
		}
		
		$this->db->delete('forums', array('parent_id' => $id));
		$this->db->delete('categories', array('id' => $id));		
	}
}